package com.bootssm.controller;

import com.bootssm.entity.*;
import com.bootssm.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {
    @Autowired
    ScheduleService scheduleService;

    @PostMapping("/findAll")
    public PageView<Schedule> findAll(@RequestBody ScheduleVO scheduleVO){
        Integer page = scheduleVO.getPage();
        Integer start = (page-1)*scheduleVO.getSize();
        scheduleVO.setStart(start);

        List<Schedule> scheduleList = scheduleService.findAll(scheduleVO);

        Integer count = scheduleService.getCount(scheduleVO);

        PageView<Schedule> pageView = new PageView<>();
        pageView.setDataList(scheduleList);
        pageView.setCount(count);
        return pageView;
    }
    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody Schedule schedule){
        String id = schedule.getScheduleid();
        scheduleService.deleteById(id);
        return new Result("200","删除成功");
    }
    @PostMapping("/update")
    public Result update(@RequestBody ScheduleVO scheduleVO) {
        try {
            scheduleService.update(scheduleVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500","修改失败");
        }
        return new Result("200","修改成功");
    }
    @PostMapping("/save")
    public Result save(@RequestBody ScheduleVO scheduleVO) {
        try {
            scheduleService.save(scheduleVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "保存失败");
        }
        return new Result("200", "保存成功");
    }

}
